from app import db
from datetime import datetime

class Menu(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    url = db.Column(db.String(200))
    icon = db.Column(db.String(50))
    parent_id = db.Column(db.Integer, db.ForeignKey('menu.id'))
    order = db.Column(db.Integer, default=0)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # 自引用关系，用于实现多级菜单
    children = db.relationship('Menu', backref=db.backref('parent', remote_side=[id]), lazy=True)

    def __repr__(self):
        return f'<Menu {self.name}>'